<template>
  <div class="login-page">
    <validate-form @form-submit="onFormSubmit">
      <div class="mb-3">
        <label class="form-label">邮箱地址</label>
        <validate-input
          :rules="emailRules"
          v-model="emailVal"
          placeholder="请输入邮箱地址"
          type="text"
          ref="inputRef"
        >
        </validate-input>
      </div>

      <div class="mb-3">
        <label class="form-label">密码</label>
        <validate-input
          :rules="passwordRules"
          v-model="passwordVal"
          type="password"
          placeholder="请输入密码"
        >
        </validate-input>
      </div>

      <template #submit>
        <span class="btn btn-danger">Submit</span>
      </template>
    </validate-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from "vue";
// route 获取路由的信息，useRouter是定义路由的一系列行为
import { useRouter } from "vue-router";
import ValidateInput, { RulesProp } from "../components/VaildateInput.vue";
import ValidateForm from "../components/ValidateForm.vue";
import { useStore } from "vuex"
export default defineComponent({
  name: "Login",

  components: {
    ValidateInput,
    ValidateForm,
  },

  setup() {
    // 邮箱双向绑定
    const emailVal = ref(null);
    const router = useRouter();
    const emailRules: RulesProp = [
      { type: "required", message: "电子邮箱地址不能为空" },
      { type: "email", message: "请输入正确的电子邮箱格式" },
    ];

    // 密码
    const passwordVal = ref(null);
    const passwordRules: RulesProp = [
      { type: "required", message: "密码不能为空" },
    ];


    // VaildateFrom的值
    const store = useStore()
    const onFormSubmit = (result: boolean) => {
      // 当邮箱和密码全部通过后 返回true 反之false
      console.log("result", result);
        
      // 验证通过之后跳转   
      if(result){
          router.push('/')
          store.commit('login');
      }
    };
    return {
      emailRules,
      emailVal,
      passwordVal,
      passwordRules,
      onFormSubmit,
    };
  },
});
</script>